package bb.lanxing.model.database;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;

import android.content.Context;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.util.JsonUtil;

public class Event extends LitePalTableBase {
    public static final String IMAGE_SEPARETOR = ";";
    public static final int LEVEL_SIMPLE = 0;
    public static final int LEVEL_RELAX = 1;
    public static final int LEVEL_HARD = 2;
    public static final int LEVEL_CRAZY = 3;
    public static final int LEVEL_HELL = 4;
    private int cityId;
    private long createTime;
    private long createUserId;
    private String description;
    private double distance;
    private long endTime;
    private long eventId;
    private int filter;
    private long groupId;
    private int joinStatus;
    private boolean isMine;
    private boolean isScored;
    private int level;
    private long lushuId = -1L;
    private String lushuTitle;
    private String lushuUuid;
    private int memberCount;
    private int memberLimit;
    private String phoneNumber;
    private String photoUrl;
    private int score;
    private String startAddr;
    private String startCity;
    private double startLat;
    private double startLng;
    private long startTime;
    private String title;
    private String TencentImGroupId;
    private long userId;
    private int cost = 0;
    private int ownerApprove;
    public static ArrayList<EventApproveUser> approveList = new ArrayList<>();
    private int locationSharing = 0;
    private boolean temp = false;

    public static int getLevelColor(int level) {
        if (level == LEVEL_RELAX) {
            return 0xFF189ADB;
        } else if (level == LEVEL_HARD) {
            return 0xFFFF9F00;
        } else if (level == LEVEL_CRAZY) {
            return 0xFFFF0000;
        } else if (level == LEVEL_HELL) {
            return 0xFF000000;
        } else {
            return 0xFF4A90E2;
        }
    }


    public static Event createByJson(JSONObject jSONObject) {
//        long longValue = JsonUtil.getLongValue("activityId", jSONObject);
        Event event = new Event();

//        event.setId(longValue);
        event.setId(null);

        event.setEventId(JsonUtil.getLongValue("eventId", jSONObject));
        event.setUserId(App.getContext().getUserId());
        event.setCreateUserId(JsonUtil.getLongValue("createUserId", jSONObject));
        event.setPhotoUrl(JsonUtil.getStringValue("photoUrl", jSONObject));
        event.setTitle(JsonUtil.getStringValue("title", jSONObject));
        event.setMemberCount(JsonUtil.getIntegerValue("memberCount", jSONObject));
        event.setMemberLimit(JsonUtil.getIntegerValue("memberLimit", jSONObject));
        event.setCityId(JsonUtil.getIntegerValue("cityId", jSONObject));
        event.setStartCity(JsonUtil.getStringValue("startCity", jSONObject));
        event.setStartAddr(JsonUtil.getStringValue("startAddr", jSONObject));
        event.setPhoneNumber(JsonUtil.getStringValue("phoneNumber", jSONObject));
        event.setDistance(JsonUtil.getDoubleValue("distance", jSONObject) / 1000.0d);
        event.setCost(JsonUtil.getIntegerValue("cost", jSONObject));
        event.setLevel(JsonUtil.getIntegerValue("level", jSONObject));
        event.setFilter(JsonUtil.getIntegerValue("activityFilterType", jSONObject));
        event.setStartTime(JsonUtil.getLongValue("startTime", jSONObject));
        event.setEndTime(JsonUtil.getLongValue("endTime", jSONObject));
        event.setOwnerApprove(JsonUtil.getIntegerValue("ownerApprove", jSONObject));

        event.setIsScored(JsonUtil.getIntegerValue("isScored", jSONObject) == 1);
        event.setJoinStatus(JsonUtil.getIntegerValue("isJoined", jSONObject));
        event.setIsMine(JsonUtil.getIntegerValue("isMine", jSONObject) == 1);
        if (jSONObject.has("description")) {
            event.setDescription(JsonUtil.getStringValue("description", jSONObject));
        }
        if (jSONObject.has("lushuId")) {
            event.setLushuId(JsonUtil.getLongValue("lushuId", jSONObject));
        }
        if (jSONObject.has("lushuUuid")) {
            event.setLushuUuid(JsonUtil.getStringValue("lushuUuid", jSONObject));
        }
        if (jSONObject.has("lushuTitle")) {
            event.setLushuTitle(JsonUtil.getStringValue("lushuTitle", jSONObject));
        }
        if (jSONObject.has("groupId")) {
            event.setGroupId(JsonUtil.getLongValue("groupId", jSONObject));
        }
        if (jSONObject.has("startLat")) {
            event.setStartLat(JsonUtil.getDoubleValue("startLat", jSONObject));
        }
        if (jSONObject.has("startLng")) {
            event.setStartLng(JsonUtil.getDoubleValue("startLng", jSONObject));
        }
        if (jSONObject.has("score")) {
            event.setScore(JsonUtil.getIntegerValue("score", jSONObject));
        }
        if (jSONObject.has("activityCreateTime")) {
            event.setCreateTime(JsonUtil.getLongValue("activityCreateTime", jSONObject));
        }
        return event;
    }

    public static Event detailCreateByJson(JSONObject jSONObject, JSONObject groupJSON, JSONArray approveJSONArray) {
        Event event = new Event();
        event.setId(null);

        event.setEventId(JsonUtil.getLongValue("eventId", jSONObject));
        event.setUserId(App.getContext().getUserId());
        event.setCreateUserId(JsonUtil.getLongValue("createUserId", jSONObject));
        event.setPhotoUrl(JsonUtil.getStringValue("photoUrl", jSONObject));
        event.setTitle(JsonUtil.getStringValue("title", jSONObject));
        event.setMemberCount(JsonUtil.getIntegerValue("memberCount", jSONObject));
        event.setMemberLimit(JsonUtil.getIntegerValue("memberLimit", jSONObject));
        event.setCityId(JsonUtil.getIntegerValue("cityId", jSONObject));
        event.setStartCity(JsonUtil.getStringValue("startCity", jSONObject));
        event.setStartAddr(JsonUtil.getStringValue("startAddr", jSONObject));
        event.setPhoneNumber(JsonUtil.getStringValue("phoneNumber", jSONObject));
        event.setDistance(JsonUtil.getDoubleValue("distance", jSONObject) / 1000.0d);
        event.setCost(JsonUtil.getIntegerValue("cost", jSONObject));
        event.setLevel(JsonUtil.getIntegerValue("level", jSONObject));
        event.setFilter(JsonUtil.getIntegerValue("activityFilterType", jSONObject));
        event.setStartTime(JsonUtil.getLongValue("startTime", jSONObject));
        event.setEndTime(JsonUtil.getLongValue("endTime", jSONObject));
        event.setOwnerApprove(JsonUtil.getIntegerValue("ownerApprove", jSONObject));
        event.setLocationSharing(JsonUtil.getIntegerValue("locationSharing", jSONObject));

        event.setIsScored(JsonUtil.getIntegerValue("isScored", jSONObject) == 1);
        event.setJoinStatus(JsonUtil.getIntegerValue("isJoined", jSONObject));
        event.setIsMine(JsonUtil.getIntegerValue("isMine", jSONObject) == 1);
        if (jSONObject.has("description")) {
            event.setDescription(JsonUtil.getStringValue("description", jSONObject));
        }
        if (jSONObject.has("lushuId")) {
            event.setLushuId(JsonUtil.getLongValue("lushuId", jSONObject));
        }
        if (jSONObject.has("lushuUuid")) {
            event.setLushuUuid(JsonUtil.getStringValue("lushuUuid", jSONObject));
        }
        if (jSONObject.has("lushuTitle")) {
            event.setLushuTitle(JsonUtil.getStringValue("lushuTitle", jSONObject));
        }
        if (jSONObject.has("groupId")) {
            event.setGroupId(JsonUtil.getLongValue("groupId", jSONObject));
        }
        if (jSONObject.has("startLat")) {
            event.setStartLat(JsonUtil.getDoubleValue("startLat", jSONObject));
        }
        if (jSONObject.has("startLng")) {
            event.setStartLng(JsonUtil.getDoubleValue("startLng", jSONObject));
        }
        if (jSONObject.has("score")) {
            event.setScore(JsonUtil.getIntegerValue("score", jSONObject));
        }
        if (jSONObject.has("activityCreateTime")) {
            event.setCreateTime(JsonUtil.getLongValue("activityCreateTime", jSONObject));
        }
        event.setTencentImGroupId(JsonUtil.getStringValue("thirdGroupId", groupJSON));
        approveList = new ArrayList<>();
        try {
            for (int i = 0; i < approveJSONArray.length(); i++) {
                JSONObject jSONObject1 = approveJSONArray.getJSONObject(i);
                EventApproveUser eventApproveUser = new EventApproveUser();
                eventApproveUser.nickName = JsonUtil.getStringValue("nickName", jSONObject1);
                eventApproveUser.userId = JsonUtil.getIntegerValue("userId", jSONObject1);
                eventApproveUser.phoneNumber = JsonUtil.getStringValue("phoneNumber", jSONObject1);
                eventApproveUser.avatar = JsonUtil.getStringValue("avatar", jSONObject1);
                approveList.add(eventApproveUser);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return event;
    }

    public static Event getByEventId(long j) {
        String eventId = String.valueOf(j);
        Event e = LitePal.where("eventid=?", eventId).findFirst(Event.class);
        return e;
    }

    public static Event getLastUncommit() {
        String user_id = String.valueOf(App.getContext().getUserId());

        Event e = LitePal.where("userId = ? and temp > 0", user_id).order("id DESC").findFirst(Event.class);

        return e;
    }

    public static List<Event> getLocationSharingEvent(int i) {
        List<Event> list_e = LitePal.where("locationSharing = ?", "1").limit(i).find(Event.class);
        return list_e;
    }

    public static String getLevelName(Context context, int i) {
        return context.getResources().getStringArray(R.array.event_level)[i];
    }

    public int getOwnerApprove() {
        return ownerApprove;
    }

    public void setOwnerApprove(int ownerApprove) {
        this.ownerApprove = ownerApprove;
    }

    public int getLocationSharing() {
        return locationSharing;
    }

    public void setLocationSharing(int locationSharing) {
        this.locationSharing = locationSharing;
    }

    public boolean isTemp() {
        return this.temp;
    }

    public void setEventId(long j) {
        this.eventId = j;
    }

    public void setUserId(long j) {
        this.userId = j;
    }

    public void setCreateUserId(long j) {
        this.createUserId = j;
    }

    public void setPhotoUrl(String str) {
        this.photoUrl = str;
    }

    public void setTitle(String str) {
        this.title = str;
    }

    public void setMemberCount(int i) {
        this.memberCount = i;
    }

    public void setMemberLimit(int i) {
        this.memberLimit = i;
    }

    public void setCityId(int i) {
        this.cityId = i;
    }

    public void setStartCity(String str) {
        this.startCity = str;
    }

    public void setStartAddr(String str) {
        this.startAddr = str;
    }

    public void setPhoneNumber(String str) {
        this.phoneNumber = str;
    }

    public void setDistance(double d) {
        this.distance = d;
    }

    public void setCost(int i) {
        this.cost = i;
    }

    public void setLevel(int i) {
        this.level = i;
    }

    public void setFilter(int i) {
        this.filter = i;
    }

    public void setStartTime(long j) {
        this.startTime = j;
    }

    public void setEndTime(long j) {
        this.endTime = j;
    }

    public void setIsScored(boolean z) {
        this.isScored = z;
    }

    public void setJoinStatus(int z) {
        joinStatus = z;
    }

    public void setIsMine(boolean z) {
        this.isMine = z;
    }

    public void setDescription(String str) {
        this.description = str;
    }

    public void setLushuId(long j) {
        this.lushuId = j;
    }

    public void setLushuUuid(String str) {
        this.lushuUuid = str;
    }

    public void setLushuTitle(String str) {
        this.lushuTitle = str;
    }

    public void setGroupId(long j) {
        this.groupId = j;
    }

    public void setStartLat(double d) {
        this.startLat = d;
    }

    public void setStartLng(double d) {
        this.startLng = d;
    }

    public void setScore(int i) {
        this.score = i;
    }

    public void setCreateTime(long j) {
        this.createTime = j;
    }

    public void setTemp(boolean z) {
        this.temp = z;
    }

    public boolean getTemp() {
        return this.temp;
    }

    public long getEventId() {
        return this.eventId;
    }

    public long getUserId() {
        return this.userId;
    }

    public long getCreateUserId() {
        return this.createUserId;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public String getTitle() {
        return this.title;
    }

    public int getMemberCount() {
        return this.memberCount;
    }

    public int getMemberLimit() {
        return this.memberLimit;
    }

    public int getCityId() {
        return this.cityId;
    }

    public String getStartCity() {
        return this.startCity;
    }

    public String getStartAddr() {
        return this.startAddr;
    }

    public String getPhoneNumber() {
        return this.phoneNumber;
    }

    public double getDistance() {
        return this.distance;
    }

    public int getCost() {
        return this.cost;
    }

    public int getLevel() {
        return this.level;
    }

    public int getFilter() {
        return this.filter;
    }

    public long getStartTime() {
        return this.startTime;
    }

    public long getEndTime() {
        return this.endTime;
    }

    public boolean isScored() {
        return this.isScored;
    }

    public int getJoinStatus() {
        return joinStatus;
    }

    public boolean isMine() {
        return this.isMine;
    }

    public String getDescription() {
        return this.description;
    }

    public long getLushuId() {
        return this.lushuId;
    }

    public String getLushuUuid() {
        return this.lushuUuid;
    }

    public String getLushuTitle() {
        return this.lushuTitle;
    }

    public long getGroupId() {
        return this.groupId;
    }

    public double getStartLat() {
        return this.startLat;
    }

    public double getStartLng() {
        return this.startLng;
    }

    public int getScore() {
        return this.score;
    }

    public long getCreateTime() {
        return this.createTime;
    }

    public String getTencentImGroupId() {
        return TencentImGroupId;
    }

    public void setTencentImGroupId(String tencentImGroupId) {
        TencentImGroupId = tencentImGroupId;
    }
}